package com.his.controller;

import com.his.annotation.RequireRoles;
import com.his.common.Const;
import com.his.common.Rsp;
import com.his.entity.Record;
import com.his.service.RecordService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Record)表控制层
 *
 * @author makejava
 * @since 2020-10-23 18:16:13
 */
@RestController
@RequestMapping("record")
@Validated
public class RecordController {
    /**
     * 服务对象
     */
    @Resource
    private RecordService recordService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Record selectOne(Integer id) {
        return this.recordService.queryById(id);
    }

    /**
     * @param token
     * @param patientId
     * @return
     */
    @GetMapping("selectOneByPatientId")
    @RequireRoles(roles = {"doctor", "patient"}, index = 0)
    public Rsp selectOneByPatientId(@RequestHeader(Const.TOKEN) String token, String patientId) throws Exception {
        return Rsp.success(recordService.selectOneByPatientId(patientId,token));
    }

    @PostMapping("insertOne")
    @RequireRoles(roles = {"doctor", "patient"}, index = 0)
    public Rsp insertOne(@RequestHeader(Const.TOKEN) String token,@Validated @RequestBody Record record) throws Exception {
        return recordService.insertOne(record,token);
    }

    @PostMapping("updateOne")
    @RequireRoles(roles = {"doctor"}, index = 0)
    public Rsp updateOne(@RequestHeader(Const.TOKEN) String token, @RequestBody Record record) {
        return Rsp.success(recordService.updateOne(record));
    }
}
